import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../hj_model/cartoon_media_info.dart';
import '../../../hj_model/home/home_video_library.dart';
import '../../../hj_model/home/module_detail_model.dart';
import '../../../hj_model/video_model.dart';
import '../../../hj_utils/api_service/vid_service.dart';
import '../../../tools_base/debug_log.dart';
import '../../../tools_base/loading/loading_alert_widget.dart';

class VideoAllTypeLogic extends GetxController with GetSingleTickerProviderStateMixin {
  VideoAllTypeLogic();

  HomeVideoLibrary? homeVideoLibrary;
  TimeType? firstSelectedType;
  TimeType? secondSelectedType;
  Tags? thirdSelectedType;
  TimeType? forthSelectedType;
  TimeType? fivthSelectedType;
  RefreshController? refreshController;
  List<VideoModel>? searchVideoList;
  List<CartoonMediaInfo>? searchACGList;
  Keyword? preKeyword;
  int currentPage = 1;
  RxBool isInitData = true.obs;

  List<Tags>? get tags {
    if(firstSelectedType?.isACG == true){
      return homeVideoLibrary?.acgTags;
    }else {
      return homeVideoLibrary?.vidTags;
    }
  }

  Keyword get keywordValue {
    Keyword keyword = Keyword();
    keyword.tags = thirdSelectedType;
    keyword.canvas = firstSelectedType;
    keyword.orderBy = secondSelectedType;
    keyword.paymentType = forthSelectedType;
    keyword.timeType = fivthSelectedType;
    return keyword;
  }

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadMenuData();
    });
  }

  void reloadMenuData() {
    isInitData.value = true;
    _loadMenuData();
  }

  void _loadMenuData() async {
    try {
      homeVideoLibrary = await VidService.fetchVideoLibrary();
      firstSelectedType = homeVideoLibrary?.canvas?.first;
      secondSelectedType = homeVideoLibrary?.orderBy?.first;
      thirdSelectedType = homeVideoLibrary?.vidTags?.first;
      forthSelectedType = homeVideoLibrary?.paymentType?.first;
      fivthSelectedType = homeVideoLibrary?.timeType?.first;
      _loadSearchData();
    } catch (e) {
      debugLog(e);
    }
    homeVideoLibrary ??= HomeVideoLibrary();
    isInitData.value = false;
    update();
  }

  void reloadData() {
    searchVideoList = null;
    update();
    _loadSearchData();
  }

  void refreshData() {
    _loadSearchData();
  }

  void loadMoreData() {
    _loadSearchData(page: currentPage + 1);
  }

  void menuExchangeEvent() {
    update();
    _loadSearchData(isSelectMenu: true);
  }

  void _loadSearchData({int page = 1, int size = 10, bool isSelectMenu = false}) async {
    try {
      if (isSelectMenu) {
        LoadingAlertWidget.show(Get.context!);
      }
      Keyword keywordParam = keywordValue;
      HomeVideoLibraryResult? respResult = await VidService.fetchVidLibraryBySearch(
        page,
        size,
        filterMenu: keywordParam,
      );
      if (keywordValue.modelKey != keywordParam.modelKey) {
        return;
      }
      if (isSelectMenu) {
        LoadingAlertWidget.cancel(Get.context!);
      }
      searchVideoList ??= [];
      searchACGList ??= [];
      currentPage = page;
      if (page == 1) {
        searchVideoList?.clear();
        searchACGList?.clear();
      }
      searchVideoList?.addAll(respResult?.list ?? []);
      searchACGList?.addAll(respResult?.allMediaList ?? []);
      if (respResult?.hasNext == true) {
        refreshController?.loadComplete();
      } else {
        refreshController?.loadNoData();
      }
    } catch (e) {
      if (isSelectMenu) {
        LoadingAlertWidget.cancel(Get.context!);
      }
      debugLog(e);
      refreshController?.loadComplete();
    }
    refreshController?.refreshCompleted();
    searchVideoList ??= [];
    update();
  }
}
